<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of EaExcelPrinter
 *
 * @author EaDeveloper
 */
class EaPdfPrinter implements EasoftPrinter {

    public static $ORIENTACION = "portrait";
    public static $PAPER = "a4";
    public static $PORTRAIT = "P";
    public static $LANDSCAPE = "L";
    public static $TAMA_FONT = 9;

    public function write($report) {
        $usr =$_SESSION[USUARIO];
        // create new PDF document
        $doc = new EasoftPDFDoc("L", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        // set document information
        $doc->SetCreator('EA-SOFTWARE');
        $doc->SetAuthor('EaSoftCollegev3');
        $doc->SetTitle($report->label);
        $doc->SetSubject($report->subtitle);
        $doc->SetKeywords('easoftware,reportmaker');

        // set default header data
        $doc->SetHeaderData(LOGO_PATH, PDF_HEADER_LOGO_WIDTH, 'Reporte: ' . $report->label, 'Usuario: ' . $usr["usuario"]."\nFecha: ".date("d-m-Y"));
        
        // set header and footer fonts
        $doc->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $doc->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        // set default monospaced font
        $doc->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        //set margins
        $doc->SetMargins(5, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $doc->SetHeaderMargin(PDF_MARGIN_HEADER);
        $doc->SetFooterMargin(PDF_MARGIN_FOOTER);

        //set auto page breaks
        $doc->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        //set image scale factor
        $doc->setImageScale(PDF_IMAGE_SCALE_RATIO);

        //set some language-dependent strings
        //$doc->setLanguageArray($l);
        // ---------------------------------------------------------
        // set font
        $doc->SetFont('helvetica', '', 7);

        // add a page
        $doc->AddPage();


        $doc->ColoredTable($report->fields, $report->lastResult);
        $doc->Output('reporte.pdf', 'D');
    }

}

class EasoftPDFDoc extends TCPDF {

    // Colored table
    public function ColoredTable($fields, $data) {
        // Colors, line width and bold font
        $this->SetTextColor(255);
        $this->SetLineWidth(0.3);
        $this->SetFont('', 'B');
        $w = array();
        // Header                
        foreach ($fields as $f) {
            $this->Cell($f['width'], 7, $f['label'], 1, 0, 'C', 1);
            $w[] = $f['width'];
        }
        $this->Ln();
        // Color and font restoration
        $this->SetFillColor(224, 235, 255);
        $this->SetTextColor(0);
        $this->SetFont('');
        // Data
        $fill = 0;
        foreach ($data as $row) {
            foreach ($fields as $f) {
                if ($f['type'] == ReportDoc::$TIPO_LETRA) {
                    $this->Cell($f['width'], 6, $row[$f['name']], 'LR', 0, 'L', $fill);
                } else {
                    $this->Cell($f['width'], 6, number_format($row[$f['name']]), 'LR', 0, 'R', $fill);
                }
            }
            $this->Ln();
            $fill = !$fill;
        }
        $this->Cell(array_sum($w), 0, '', 'T');
    }

}

?>